﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FourKill.Engine
{
    public class Placement : Coordinates3D, IPlacement, ICloneable
    {
        private double _height, _width, _bearing, _totalMovement;
        private RectangleF _rectangle;

        public Placement() { }

        public void Move(double distance)
        {
            X += distance * Math.Cos(Bearing);
            Y += distance * Math.Sin(Bearing);
            _totalMovement += distance;
        }

        public void SideStep(double distance)
        {
            if (distance < 0)
            {
                X += distance * Math.Cos(Bearing + Utility._270);
                Y += distance * Math.Sin(Bearing + Utility._270);
            }
            else if (distance > 0)
            {
                X += distance * Math.Cos(Bearing - Utility._90);
                Y += distance * Math.Sin(Bearing - Utility._90);
            }
        }

        public void Turn(double radians)
        {
            Bearing += radians;
            while (Bearing < 0) Bearing = Bearing + Utility._360;
            if (Bearing >= Utility._360) Bearing = (Bearing % Utility._360);
        }

        public double Bearing
        {
            get { return _bearing; }
            set { _bearing = value; }
        }

        public double TotalMovement
        {
            get { return _totalMovement; }
            set { _totalMovement = value; }
        }

        public double BearingDegrees
        {
            get { return Utility.Degrees(_bearing); }
        }

        public RectangleF RectangleF
        {
            get { return _rectangle; }
            set { X = _rectangle.X; Y = _rectangle.Y; Height = _rectangle.Height; Width = _rectangle.Width; }
        }

        public virtual double Height
        {
            get { return _height;}
            set { _height = value; _rectangle.Height = Convert.ToSingle(_height); }
        }

        public virtual double Width
        {
            get { return _width; }
            set { _width = value; _rectangle.Width = Convert.ToSingle(_width); }
        }

        public override double X
        {
            get { return base.X; }
            set { base.X = value; _rectangle.X = PointF.X; }
        }

        public override double Y
        {
            get { return base.Y; }
            set { base.Y = value; _rectangle.Y = PointF.Y; }
        }

        public new object Clone()
        {
            return Placement.Clone(this);
        }

        public void CopyInto(IPlacement p)
        {
            p.Bearing = this.Bearing;
            p.AngleFromCamera = this.AngleFromCamera;
            p.Height = this.Height;
            p.Width = this.Width;
            p.X = this.X;
            p.Y = this.Y;
            p.Z = this.Z;
            p.TotalMovement = this.TotalMovement;
        }

        public static Placement Clone(IPlacement c)
        {
            Placement p = new Placement();
            c.CopyInto(p);
            return p;
        }

        public static void CopyInto(IPlacement source, IPlacement destination)
        {
            source.CopyInto(destination);
        }
    }
}
